package com.pa165.languageschoolpresentation.rest;

import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolservice.DTO.CourseDTO;
import com.pa165.languageschoolservice.service.CourseService;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Tomas Hruby
 */
@Component
@Path("/courses")
public class CourseRestService {

    @Autowired
    private CourseService courseService;

    public CourseService getCourseService() {
        return courseService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    @GET
    @Path("/getCourse")
    @Produces(MediaType.APPLICATION_JSON)
    public CourseDTO get(@QueryParam("id") Long id) {
        CourseDTO course;

        try {
            course = courseService.get(id);
        } catch (IllegalArgumentPersistenceException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        if (course == null) {
            // 404
            throw new WebApplicationException(
                    Response.status(Response.Status.NOT_FOUND)
                    .entity("Course with id " + id + " wasn't found")
                    .build());
        }
        return course;
    }

    @GET
    @Path("/getAllCourses")
    @Produces(MediaType.APPLICATION_JSON)
    public List<CourseDTO> getAll() {
        List<CourseDTO> list = courseService.getAll();
        if (list != null) {
            return list;
        } else {
            // 404
            throw new WebApplicationException(
                    Response.status(Response.Status.NOT_FOUND)
                    .entity("getAll return null list")
                    .build());
        }
    }

    @GET
    @Path("/getCoursesByName")
    @Produces(MediaType.APPLICATION_JSON)
    public List<CourseDTO> getByName(@QueryParam("name") String name) {
        List<CourseDTO> result = new ArrayList<CourseDTO>();

        try {
            result = courseService.findByName(name);
        } catch (IllegalArgumentPersistenceException ex) {
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        return result;
    }

    // Implement search by language or not ?
    @POST
    @Path("/createCourse")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response add(CourseDTO course) {
        try {
            courseService.add(course);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        String result = "Course created : " + course;
        // 200
        return Response.ok(result).build();
    }

    @POST
    @Path("/updateCourse")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response modify(CourseDTO course) {
        try {
            courseService.modify(course);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        String result = "Course updated : " + course;
        // 200
        return Response.ok(result).build();
    }

    @DELETE
    @Path("/deleteCourse")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteCourse(@QueryParam("id") Long id) {
        CourseDTO course;

        try {
            course = courseService.get(id);
        } catch (IllegalArgumentPersistenceException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        if (course == null) {
            // 404
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        try {
            courseService.remove(course);
        } catch (IllegalArgumentPersistenceException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        String result = "Course removed : " + course;
        // 200
        return Response.ok(result).build();
    }
}
